﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.ModuleVersions
{
    public class ModuleVersions
    {
        public static void InsertModuleVersion(string moduleName,ref Model.NutriLeafDBEntities context)
        {
            if (!Exists(moduleName, ref context))
            {
                Model.ModuleVersion module = new Model.ModuleVersion();
                module.VersionName = moduleName;
                module.VersionNumber = 1;
                context.ModuleVersions.AddObject(module);
                context.SaveChanges();
            }
        }

        public static bool Exists(string name, ref Model.NutriLeafDBEntities context)
        {
            if (context.ModuleVersions.Select(o => o.VersionName == name).Count() == 0)
            {
                return false;
            }
            return true;
        }

        public static void DeleteModule(string name, ref Model.NutriLeafDBEntities context)
        {
            Model.ModuleVersion module = context.ModuleVersions.SingleOrDefault(o => o.VersionName == name);
            if (module != null)
            {
                context.ModuleVersions.DeleteObject(module);
                context.SaveChanges();
            }
        }

        public static int GetVersion(string name, ref Model.NutriLeafDBEntities context)
        {
            Model.ModuleVersion module = GetModule(name, ref context);
            if (module != null)
            {
                return (int)module.VersionNumber;
            }
            else
            {
                return -1;
            }
        }
        public static void UpdateVersion(string name, int number, ref Model.NutriLeafDBEntities context)
        {
            Model.ModuleVersion module = GetModule(name, ref context);
            if (module != null)
            {
                module.VersionNumber = number;
                context.SaveChanges();
            }
        }

        public static void IncreaseVersion(string name, ref Model.NutriLeafDBEntities context)
        {
            Model.ModuleVersion module = GetModule(name, ref context);
            if (module != null)
            {
                module.VersionNumber++;
                context.SaveChanges();
            }
        }

        public static void DecreaseVersion(string name, ref Model.NutriLeafDBEntities context)
        {
            Model.ModuleVersion module = GetModule(name, ref context);
            if (module != null)
            {
                module.VersionNumber--;
                context.SaveChanges();
            }
        }

        public static Model.ModuleVersion GetModule(string name, ref Model.NutriLeafDBEntities context)
        {
            return context.ModuleVersions.SingleOrDefault(o => o.VersionName == name);
        }

        public static bool IsLatest(string name, int version,ref Model.NutriLeafDBEntities context)
        {
            Model.ModuleVersion module = GetModule(name, ref context);

            if (module != null)
            {
                if (module.VersionNumber <= version)
                {
                    return true;
                }
            }

            return false;
        }


    }
}